/**
  ******************************************************************************
  * @file    dev_flash.c
  * @author  Yun
  * @version V1.0.0
  * @date    2025-02-13
  * @brief   FLASH设备驱动模块实现
  * @license EULA (End User License Agreement)
  *          Copyright (c) 2025 星瑜科技自动化 Co., Ltd. All rights reserved.
  ******************************************************************************
  */

/*---------------------- Includes ----------------------*/
#include "dev_flash.h"

/*---------------------- 私有变量定义 ----------------------*/
static Dev_Flash_Config_TypeDef stDevFlashCfg;          /*!< FLASH设备配置实例 */

/*---------------------- 函数实现 ----------------------*/
/**
  * @brief  FLASH设备注册函数
  */
uint8_t Dev_Flash_Register(Flash_Interface_TypeDef* pstDevFlashInterface,
                           uint32_t u32FlashBaseCfg,
                           uint32_t u32FlashPageSizeCfg,
                           uint32_t u32FlashSizeCfg)
{
    /* 参数有效性检查 */
    if (!pstDevFlashInterface)
        return DEV_ERROR;

    DEV_UN_INIT((&stDevFlashCfg));

    /* 配置设备接口 */
    stDevFlashCfg.pstDevFlashInterface = pstDevFlashInterface;

    /* 执行硬件初始化 */
    CHECK_DEV_STATUS(stDevFlashCfg.pstDevFlashInterface->Init(u32FlashBaseCfg,
                                                                  u32FlashPageSizeCfg,
                                                                  u32FlashSizeCfg));
    DEV_INIT((&stDevFlashCfg));
    return DEV_OK;
}

/**
  * @brief  FLASH数据读取
  */
uint8_t Dev_Flash_Read(uint32_t u32Addr,
                       void* pBuffer,
                       uint32_t u32Len,
                       uint8_t  u8Mode)
{
    /* 参数有效性检查 */
    if (!pBuffer || u32Len == 0 ||
    (u8Mode > DEV_FLASH_DATA_U32))
        return DEV_ERROR;

    CHECK_DEV_ENABLE((&stDevFlashCfg));
    DEV_BUSY((&stDevFlashCfg));

    CHECK_DEV_STATUS(stDevFlashCfg.pstDevFlashInterface->Read(u32Addr,
                                                                  pBuffer,
                                                                  u32Len,
                                                                  u8Mode));
    return DEV_OK;
}

/**
  * @brief  FLASH数据擦除
  */
uint8_t Dev_Flash_Erase(uint32_t u32Addr, uint32_t u32Len)
{
    CHECK_DEV_ENABLE((&stDevFlashCfg));
    DEV_BUSY((&stDevFlashCfg));

    CHECK_DEV_STATUS(stDevFlashCfg.pstDevFlashInterface->Erase(u32Addr,
                                                                   u32Len));
    return DEV_OK;
}

/**
  * @brief  FLASH数据写入
  */
uint8_t Dev_Flash_Write(uint32_t u32Addr,
                        void* pBuffer,
                        uint32_t u32Len,
                        uint8_t  u8Mode)
{
    /* 参数有效性检查 */
    if (!pBuffer || u32Len == 0 ||
        (u8Mode > DEV_FLASH_DATA_U32))
        return DEV_ERROR;

    CHECK_DEV_ENABLE((&stDevFlashCfg));
    DEV_BUSY((&stDevFlashCfg));

    CHECK_DEV_STATUS(stDevFlashCfg.pstDevFlashInterface->Write(u32Addr,
                                                                   pBuffer,
                                                                   u32Len,
                                                                   u8Mode));

    return DEV_OK;
}

/************************ (C) COPYRIGHT 星瑜科技自动化 *****END OF FILE****/
